package com.woniuxy.controller;


import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.common.Result;
import com.woniuxy.controller.form.*;
import com.woniuxy.entity.StoreRegister;
import com.woniuxy.service.StoreRegisterService;
import com.woniuxy.service.dto.FoodForEsDto;
import com.woniuxy.service.dto.StoreInfoForEsDto;
import com.woniuxy.service.dto.StoreRegisterDto;
import com.woniuxy.service.dto.WoniuPageDto;
import com.woniuxy.util.GetTypeByHead;
import com.woniuxy.util.OSSUtil;
import com.woniuxy.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 商家注册表 前端控制器
 * </p>
 *
 * @author zz
 * @since 2023年06月13日
 */
@RestController
@RequestMapping("/storeRegister")
@Api(tags = "商家注册相关接口")
public class StoreRegisterController {
    @Autowired
    private StoreRegisterService service;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private OSSUtil ossUtil;
    @Autowired
    private ElasticsearchRestTemplate template;
    @PostMapping("/add")
    @ApiOperation("添加商家申请")
    public Result register(@RequestBody StoreRegisterForm form ,@RequestHeader String accessToken) throws Exception {
        Integer accountId = redisUtil.getUserIdByToken(accessToken);
        service.addRegister(form,accountId);
        return Result.success(200,"添加商家申请成功",null);
    }

    @PostMapping("/update")
    @ApiOperation("修改商家申请")
    public Result updateMessage(@RequestBody StoreRegisterUpdateForm form , @RequestHeader String accessToken) throws Exception {
        Integer accountId = redisUtil.getUserIdByToken(accessToken);
        StoreRegister storeRegister =service.updateMessage(form,accountId);
        return Result.success(200,"修改商家申请成功",storeRegister);
    }

    @GetMapping("/check")
    @ApiOperation("查询商铺名称重名")
    @ApiImplicitParam(name = "storeName",value = "店铺名称")
    public Result check( String storeName) throws Exception {
        String result =service.check(storeName);
        return Result.success(200,"查询商家重名成功",result);
    }




    @PostMapping("/upload")
    @ApiOperation("上传注册图片")
    public Result upload(MultipartFile file) throws Exception {
        String fileType = GetTypeByHead.getFileHeader(file);
        if ( !(fileType.equals("jpg")|| fileType.equals("png")|| fileType.equals("tif") ||fileType.equals("jpeg"))){
            throw new Exception("请上传图片类型的文件");
        }
        InputStream is = file.getInputStream();
        String fileOriginalFilename = file.getOriginalFilename();
        String suffix = FileUtil.getSuffix(fileOriginalFilename);
        String newFileName = UUID.randomUUID().toString().replace("_","");
        String url = ossUtil.uploadFile(is, newFileName + "." + suffix, "bucket-book-fjx");
        return Result.success(200,"上传图片成功",url);
   //https://bucket-book-fjx.oss-cn-chengdu.aliyuncs.com/606b7e7c-addf-4e65-8b2f-7e4bea2b4157.jpeg
    }

    @PostMapping("/search")
    @ApiOperation("查询某状态的注册详情")
    public Result searchByStatus(@RequestBody StoreRegisterSearchForm form) {
        WoniuPageDto<StoreRegisterDto> dtoList =service.searchByStatus(form);
        return Result.success(200,"查询注册成功",dtoList);
    }

    @PostMapping("/updateStatus")
    @ApiOperation("审核商家申请")
    public Result updateStatus(@RequestBody StoreRegisterUpdateStatusForm form) throws Exception {
        StoreRegister storeRegister =service.updateStatus(form);
        return Result.success(200,"审核注册成功",storeRegister);
    }

    @GetMapping("/checkFormed")
    @ApiOperation("查询是否提交过注册表")
    public Result checkFormed( @RequestHeader String accessToken) throws Exception {
        Integer accountId = redisUtil.getUserIdByToken(accessToken);
        Integer formed =service.checkFormed(accountId);
        return Result.success(200,"查询是否提交成功",formed);
    }


    @GetMapping("/message")
    @ApiOperation("查询当前用户注册信息")
    public Result message( @RequestHeader String accessToken) throws Exception {
        Integer accountId = redisUtil.getUserIdByToken(accessToken);
        StoreRegister register = service.getOne(new QueryWrapper<StoreRegister>().eq("accountId", accountId));
        return Result.success(200,"查询当前用户注册信息成功",register);
    }

    @PostMapping("/testEs")
    @ApiOperation("新增Es")
    public Result testEs() throws Exception {
//        StoreInfoForEsDto storeInfoForEsDto = new StoreInfoForEsDto();
//        storeInfoForEsDto.setStoreName("小面馆");
//        storeInfoForEsDto.setCount(20);
//        storeInfoForEsDto.setDphPrice(new BigDecimal(5));
//        storeInfoForEsDto.setStoreScore(Double.valueOf(5));
//        storeInfoForEsDto.setId(Long.valueOf(100));
//        storeInfoForEsDto.setInitiatePrice(new BigDecimal(20));
//        ArrayList<FoodForEsDto> foodForEsDtos = new ArrayList<>();
//        FoodForEsDto foodForEsDto1 = new FoodForEsDto();
//        foodForEsDto1.setFoodId(Long.valueOf(1));
//        foodForEsDto1.setFoodName("牛肉面");
//        FoodForEsDto foodForEsDto2 = new FoodForEsDto();
//        foodForEsDto2.setFoodId(Long.valueOf(2));
//        foodForEsDto2.setFoodName("羊肉面");
//        foodForEsDtos.add(foodForEsDto1);
//        foodForEsDtos.add(foodForEsDto2);
//        storeInfoForEsDto.setFood(foodForEsDtos);
//        template.save(storeInfoForEsDto);

        StoreInfoForEsDto storeInfoForEsDto = new StoreInfoForEsDto();
        storeInfoForEsDto.setStoreName("乡村基");
        storeInfoForEsDto.setCount(20);
        storeInfoForEsDto.setDphPrice(new BigDecimal(5));
        storeInfoForEsDto.setStoreScore(Double.valueOf(5));
        storeInfoForEsDto.setId(Long.valueOf(200));
        storeInfoForEsDto.setInitiatePrice(new BigDecimal(20));
        ArrayList<FoodForEsDto> foodForEsDtos = new ArrayList<>();
        FoodForEsDto foodForEsDto1 = new FoodForEsDto();
        foodForEsDto1.setFoodId(Long.valueOf(1));
        foodForEsDto1.setFoodName("牛肉水饺");
        FoodForEsDto foodForEsDto2 = new FoodForEsDto();
        foodForEsDto2.setFoodId(Long.valueOf(2));
        foodForEsDto2.setFoodName("羊肉面");
        foodForEsDtos.add(foodForEsDto1);
        foodForEsDtos.add(foodForEsDto2);
        storeInfoForEsDto.setFood(foodForEsDtos);
        template.delete(storeInfoForEsDto);

//        NativeSearchQuery nsq = new NativeSearchQueryBuilder().withIds(Arrays.asList("1")).build();
//        SearchHits<StoreInfoForEsDto> search = template.search(nsq, StoreInfoForEsDto.class);
//        List<SearchHit<StoreInfoForEsDto>> searchHits = search.getSearchHits();


        return Result.success(200,"测试成功",storeInfoForEsDto);
    }

}

